package com.caidanxiaoyuan.model.pay;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.caidanxiaoyuan.util.MD5Util;



public class PayUtil {
	
	private static String ALI_KEY = "kmwruy1fldib251awd7b8u0ba25ggdeg";
	private static String orderurl="http://www.rsseasy.com/pay/orderid.aspx";
    private static String userid="2088421936378665";
    private static String account="2088421936378665";
    private static String notify_url="http://api.caidan360.com/pay/notityurl.php";
    private static String return_url="http://localhost/pay.html";
    
    private static String ALI_INPUT_CHARSET = "utf-8";
    private static String ALI_PAYMENT_TYPE = "1";
    
    private static String service =  "alipay.wap.create.direct.pay.by.user";
	 /**
     * 支付宝封装请求参数到map
     */
	public static Map<String, String> aliorganizeRequestPara(PayModel reqInfo){
		
		Map<String, String> sParaTemp = new HashMap<String, String>();
		
		sParaTemp.put("service", service);
		
        sParaTemp.put("partner", userid);
        
        sParaTemp.put("seller_id", account);
        
        sParaTemp.put("seller_email", account);
        
        sParaTemp.put("_input_charset", ALI_INPUT_CHARSET);
        
		sParaTemp.put("payment_type", ALI_PAYMENT_TYPE);
		
		sParaTemp.put("notify_url", notify_url);
		
		sParaTemp.put("return_url", return_url);
		
		sParaTemp.put("out_trade_no", reqInfo.getOrderid());
		
		sParaTemp.put("subject", reqInfo.getMingcheng());
		
		sParaTemp.put("total_fee", reqInfo.getFee());
		
		return buildRequestPara(sParaTemp);
	}
	
	/**
     * 支付宝生成要请求给支付宝的参数数组
     */
    public static Map<String, String> buildRequestPara(Map<String, String> sParaTemp) {
    	
        //除去数组中的空值和签名参数
        Map<String, String> sPara = paraFilter(sParaTemp);
        
        //生成签名结果
        String mysign = buildRequestMysign(sPara);

        //签名结果与签名方式加入请求提交参数组中
        sPara.put("sign", mysign);
        sPara.put("sign_type", "MD5");

        return sPara;
    }
    
    /** 
     * 支付宝除去数组中的空值和签名参数
     */
    public static Map<String, String> paraFilter(Map<String, String> sArray) {

        Map<String, String> result = new HashMap<String, String>();

        if (sArray == null || sArray.size() <= 0) {
            return result;
        }

        for (String key : sArray.keySet()) {
        	
            String value = sArray.get(key);
            
            if (value == null || value.equals("") || key.equalsIgnoreCase("sign")
                || key.equalsIgnoreCase("sign_type")) {
            	
                continue;
                
            }
            
            result.put(key, value);
        }

        return result;
    }
    
    /**
     * 支付宝生成签名结果
     */
	public static String buildRequestMysign(Map<String, String> sPara) {
		
		//把数组所有元素，按照“参数=参数值”的模式用“&”字符拼接成字符串
    	String prestr = createLinkString(sPara); 
    	
        String mysign = "";
        
        	prestr += ALI_KEY;
        	
        	mysign = MD5Util.getMD5(prestr);
        	
        return mysign;
    }
	
	/** 
     * 支付宝把数组所有元素排序，并按照“参数=参数值”的模式用“&”字符拼接成字符串
     */
    public static String createLinkString(Map<String, String> params) {

        List<String> keys = new ArrayList<String>(params.keySet());
        Collections.sort(keys);

        String prestr = "";

        for (int i = 0; i < keys.size(); i++) {
        	
            String key = keys.get(i);
            String value = params.get(key);

            if (i == keys.size() - 1) {//拼接时，不包括最后一个&字符
                prestr = prestr + key + "=" + value;
            } else {
                prestr = prestr + key + "=" + value + "&";
            }
        }

        return prestr;
    }
}
